package com.hqyj.javaGraduation.hotel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.javaGraduation.hotel.dao.HotelTableDao;
import com.hqyj.javaGraduation.hotel.dao.UserTableDao;
import com.hqyj.javaGraduation.hotel.pojo.HotelTable;
import com.hqyj.javaGraduation.hotel.pojo.UserTableInfo;
import com.hqyj.javaGraduation.hotel.service.UserTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;

@Service
public class UserTableServiceImpl implements UserTableService {

    @Autowired
    private HotelTableDao hotelTableDao;
    @Autowired
    private UserTableDao userTableDao;

    // 客户预定座位
    @Override
    @Transactional
    public HashMap<String, Object> insertUserReserveTable(UserTableInfo userTableInfo) {
        HashMap<String, Object> map = new HashMap<>();
        String s = hotelTableDao.queryHotelTableByTableId(userTableInfo.getTableId());
        if(!s.equals("空闲")){
            map.put("info","该座位无法被预定");
            return map;
        }
        int i = userTableDao.insertUserReserveTable(userTableInfo);
        if(i>0){
            HotelTable table = new HotelTable();
            table.setTableState("预定");
            table.setTableId(userTableInfo.getTableId());
            hotelTableDao.updateTable(table);
            map.put("info","预定座位成功");
        }else {
            map.put("info","预定座位失败");
        }
        return map;
    }

    // 取消座位预定
    @Override
    @Transactional
    public HashMap<String, Object> deleteUserReserveTableByUserTableInfo(UserTableInfo userTableInfo) {
        HashMap<String, Object> map = new HashMap<>();
        int i = userTableDao.deleteUserReserveTableByUserTableInfo(userTableInfo);
        if(i>0){
            HotelTable table = new HotelTable();
            table.setTableState("空闲");
            table.setTableId(userTableInfo.getTableId());
            hotelTableDao.updateTable(table);
            map.put("info","取消预定座位成功");
        }else {
            map.put("info","取消预定座位失败");
        }
        return map;
    }

    // 客户在预定时间内到达
    @Override
    @Transactional
    public HashMap<String, Object> updateUserReserveTable(UserTableInfo userTableInfo) {
        HashMap<String, Object> map = new HashMap<>();
        int i = userTableDao.updateUserReserveTable(userTableInfo);
        if(i>0){
            HotelTable table = new HotelTable();
            table.setTableState("占用");
            table.setTableId(userTableInfo.getTableId());
            hotelTableDao.updateTable(table);
            map.put("info","修改客户预定信息成功");
        }else {
            map.put("info","修改客户预定信息失败");
        }
        return map;
    }

}
